package highlighter.tests;

import java.io.*;
import highlighter.parser.*;
import org.junit.Test;

public class ParserTest {

	@Test
	public void testInt() throws Exception
	{
		runParser("int i;");
	}
	
	@Test
	public void testFloat() throws Exception
	{
		runParser("float k;");
	}
	
	@Test
	public void testAssign() throws Exception
	{
		runParser("i = 2 + 4;");
	}
	
	@Test
	public void testWhile() throws Exception
	{
		runParser("while ( i > 0) { i = i -1; }");
	}
	
	@Test
	public void testIf() throws Exception
	{
		runParser("if ( j > i ) { j = 1+2+3; } else { i = 1*2+2*3+3*4 + j; }");
	}
	
	@Test
	public void testExample() throws Exception
	{
		runParser(new FileReader("input.txt"));
	}
	
	private void runParser(String sourceCode) throws Exception {
		runParser(new StringReader(sourceCode));
	}
	
	private void runParser(Reader reader) throws Exception {
		// define lexer and parser
		HighlighterLexer lexer = new HighlighterLexer(reader);
		HighlighterParser parser = new HighlighterParser(lexer);
		// parse source code
		parser.parse();
	}
}
